package com.stooscloud.provider.impl;

import com.stooscloud.MD5Util;
import com.stooscloud.RedisKeyUtil;
import com.stooscloud.base.exception.BusinessException;
import com.stooscloud.provider.builder.Builder;
import com.stooscloud.provider.config.TransInitRunner;
import com.stooscloud.provider.impl.doman.Idcheck;
import com.stooscloud.provider.impl.factory.IdCheckBuilderFactory;
import com.stooscloud.provider.model.dto.IdChkDto;
import com.stooscloud.provider.model.dto.response.IdCheckRespose;
import com.stooscloud.provider.service.IdChkFeignApi;
import com.stooscloud.wrapper.WrapMapper;
import com.stooscloud.wrapper.Wrapper;
import config.properties.WsProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class IdCheckBuilder implements Builder<Idcheck> {
    @Autowired
    private TransInitRunner transInitRunner;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    WsProperties wsProperties;
    @Autowired
    private IdCheckBuilderFactory idCheckBuilderFactory;
    @Resource
    private IdChkFeignApi idChkFeignApi;

    private Idcheck idcheck;
    private IdChkDto idChkDto;
    private IdCheckRespose idCheckRespose;

    @Override
    public Idcheck createDoman(Object obj) throws BusinessException {
        try {
            this.idcheck = idCheckBuilderFactory.builderObj(obj);
            log.info("接收前端的报文信息:{}",this.idcheck.toString());
        }catch (Exception ex){
            if(ex instanceof BusinessException){
                throw ex;
            }else {
                throw new BusinessException(1002,"解析上传报文失败!");
            }
        }
        return null;
    }

    @Override
    public void handler() throws BusinessException {
        idChkDto = new IdChkDto();
        String sign = Sign();
        BeanUtils.copyProperties(idcheck,idChkDto);
        idChkDto.setSign(sign);
    }

    private String Sign(){
        StringBuffer buffer = new StringBuffer();
        buffer.append(idcheck.getIdCode()).append(idcheck.getName()).append(wsProperties.getIdchk().getKey());
        return StringUtils.lowerCase(MD5Util.encodeByMD5(buffer.toString()));
    }
    @Override
    public void idBus() throws BusinessException {
        if (transInitRunner.busMap.get(idcheck.getSystemId()+"_"+idcheck.getTranCode())){
            log.info("开始业务逻辑处理");
        }
    }

    @Override
    public void call() throws BusinessException {
        try {
            idCheckRespose = idChkFeignApi.call(idChkDto);
        } catch (Exception e) {
            log.error("请求服务端失败",e);
            if (idCheckRespose == null)
                idCheckRespose = new IdCheckRespose();
            idCheckRespose.setResultCode("9001");
            idCheckRespose.setResultMsg("查询错误");
        }
    }

    @Override
    public Wrapper installWrapper() throws BusinessException {
        Wrapper wrapper = WrapMapper.ok(idCheckRespose);
        updateRedisMsg();
        return wrapper;
    }

    private void updateRedisMsg(){
        redisTemplate.opsForValue().set(RedisKeyUtil.getIdChkElement(idCheckRespose.getResultCode(),idcheck.getTrans_id()),idCheckRespose);
    }
}
